//define定义一个头部模块， 以便在index.js的地方require来获取
define(['jquery','cookie'],function(){
    class Header{
        constructor(){
            this.loadHeader().then(()=>{
                {
                    this.show()
                    this.search()
                    this.suanCar()
                    this.show_top_nav()
                    this.getCookie()
                   
                    

                   

                   
                }
            })
           
        }
        loadHeader(){
            return  new Promise(function(resolve,reject){
                $('header').load('/html/modules/header.html',()=>{
                    resolve()
                })
    
            })
          
        }

      
      
        show(){
            $('#search').on('click',()=>{
                $('#txt').animate({width:'toggle'},850)
                $('#txt').val("")

            })
        }
        search(){
            $('#txt').on('keyup',function(){
                const wd= $(this).val()
                if(wd){
                    $.getJSON("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?cb=?",{wd},resp=>{
                        var list = resp.s;
                            var str = "";
                            list.forEach(item => {
                                str += `<li class="search_list">${item}</li>`
                            })
                            $('#list').show()
                            $("#list").html(str);
                    }
                    )
                }
            })
            $('#txt').on('blur',function(){
                $('#list').hide()
            })
            $('#list').on('mousedown','li',function(){
                $('#txt').val($(this).html())
            })
        }
        suanCar(){
            let totalCart=0
            let cart=localStorage.getItem('cart')
            if(cart){
                cart=JSON.parse(cart)
                
                totalCart=cart.reduce((num,shoes)=>{
                    num+=+(shoes.count)
                    return num
                   
                },0)
                // return totalCart


            }
            $('#cart-num').html(totalCart)
            $('#header #cart-num').html(totalCart)


            $('.flycar-num').html(totalCart)
            $('.topbox p span').html(totalCart)



        }
        show_top_nav() {
            $(document).scroll(function() {
                var res = $(this).scrollTop();
                if ($(document).scrollTop() > 200) {
                    $("#header").fadeIn(1000)
                    $('#nav').fadeIn(1000);
                } else {
                    $("#header").fadeOut(1000);
                    $("#nav").fadeOut(1000);
                }
            })
        }
       
          getCookie(){
                var name=$.cookie('login_user')
                // console.log(name)
                if(name){
                    $('.cookie span').html(name)
                    $('#header .cookie span').html(name) 
                }
            else{
                $('.cookie').hide()
              
            }
          

          }

       
    }return new Header()
})
